package com.becca.cake.service.recommend.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.becca.cake.core.exception.ServiceException;
import com.becca.cake.dao.recommend.RecommendDAO;
import com.becca.cake.model.goods.GoodsRecommendModel;
import com.becca.cake.service.recommend.IRecommendService;
import com.becca.cake.util.JsonUtil;

/**
 * 商品推荐service实现类
 * @copyright  尊星(上海)信息科技有限公司
 * @author yellow
 * @date 2016年6月2日 下午7:16:40
 */
@Service
public class RecommendServiceImpl implements IRecommendService {
    @Autowired
    private RecommendDAO recommendDAO;

    @Override
    public List<GoodsRecommendModel> selectRecommend(Map<String, Object> map)
                                                                             throws ServiceException {
        return this.recommendDAO.selectRecommends(map);
    }

    @Override
    public Long selectRecommendCount(Map<String, Object> map) throws ServiceException {
        return this.recommendDAO.selectRecommendsCount(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateOrders(String jsonList) throws ServiceException {
        List<GoodsRecommendModel> list = JsonUtil.returnObjectList(jsonList,
            GoodsRecommendModel.class);
        GoodsRecommendModel.validRecommendOrders(list);
        this.recommendDAO.updateOrders(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateRecommendStatusById(Integer recommendId, Integer status,
                                          Integer recommendisHot, String trueName)
                                                                                  throws ServiceException {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("recommendId", recommendId);
        paramMap.put("status", status);
        paramMap.put("userName", trueName);
        if (GoodsRecommendModel.RECOMMENT_ISHOT_YES.toString().equals(status)) {
            // 不推荐 同时取消热门
            paramMap.put("recommendisHot", GoodsRecommendModel.RECOMMENT_ISHOT_NO);
        } else {
            paramMap.put("recommendisHot", recommendisHot);
        }
        this.recommendDAO.updateRecommendStatusById(paramMap);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateRecommendIsHotById(Map<String, Object> map) throws ServiceException {
        this.recommendDAO.updateRecommendIsHotById(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void insertRecommend(GoodsRecommendModel goodsRecommendModel, String trueName)
                                                                                         throws ServiceException {
        // 1.step 校验添加推荐
        GoodsRecommendModel.validInsertGoodsRecommend(goodsRecommendModel);
        goodsRecommendModel.setIsHot(GoodsRecommendModel.RECOMMENT_ISHOT_NO);
        goodsRecommendModel.setRecommendStatus(GoodsRecommendModel.RECOMMENT_STATUS_NO);
        goodsRecommendModel.setCreatedName(trueName);
        // 2.step 判断该推荐是否已存在
        Long isExist = this.recommendDAO.selectRecommendById(goodsRecommendModel);
        if (isExist != 0) {
            throw new ServiceException(GoodsRecommendModel.RECOMMENT_GOODS_EXIST);
        }
        this.recommendDAO.insertRecommend(goodsRecommendModel);
    }

}
